package opal.controller;

import opal.service.ToolServiceI;
import opal.model.Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * Created by dongsidou on 2016/8/26.
 */

@Controller
@RequestMapping("/tool")
public class ToolController {

    private ToolServiceI toolService;

    public ToolServiceI getUserService() {
        return toolService;
    }

    @Autowired
    public void setToolService(ToolServiceI toolService) {
        this.toolService = toolService;
    }



    //工具列表
    @RequestMapping("/list")
    public String toollist(Model model, HttpServletRequest request){
        List<Tool> toolList= toolService.getAll();
        model.addAttribute("toollist",toolList);
        return "toollist";
    }

    @RequestMapping("/detail/{toolid}")
    public  String tooldetail(@PathVariable Integer toolid,Model model,HttpServletRequest request){
        Tool tool = toolService.getToolById(toolid);
        request.setAttribute("tool",tool);
        return "tooldetail";
    }

    //删除工具
    @RequestMapping("/delete/{toolid}")
    public String  toolDelete(@PathVariable Integer toolid, Model model, HttpServletRequest request){
        toolService.deleteToolById(toolid);
        List<Tool> toolList=toolService.getAll();
        model.addAttribute("toollist",toolList);
        return "toollist";
    }

    @RequestMapping("/empty")
    public String toolempty(HttpServletRequest request){
        Tool tool = new Tool();
        request.setAttribute("tool",tool);
        return "tooladd";
    }

    //增加工具
    @RequestMapping("/add")
    public  String tooladd(Model model ,HttpServletRequest request){
        Tool tool =new Tool();
        tool.setToolname(request.getParameter("toolname"));
        tool.setTooldes(request.getParameter("tooldes"));
        tool.setUrl(request.getParameter("toolurl"));
        tool.setProcessid(Integer.parseInt(request.getParameter("processid")));
        toolService.insert(tool);
        List<Tool> toolList=toolService.getAll();
        model.addAttribute("toollist",toolList);
        return "toollist";
    }



    //修改工具信息
    @RequestMapping("/edit/{toolid}")
    public String tooledit(@PathVariable Integer toolid, Model model, HttpServletRequest request){
        Tool tool = toolService.getToolById(toolid);
        tool.setToolname(request.getParameter("toolname"));
        tool.setTooldes(request.getParameter("tooldes"));
        tool.setUrl(request.getParameter("toolurl"));
        tool.setProcessid(Integer.parseInt(request.getParameter("processid")));
        toolService.updateTool(tool);
        List<Tool> toolList=toolService.getAll();
        model.addAttribute("toollist",toolList);
        return "toollist";
    }



}
